/*
 * This file is part of the GUI_SDK.
 *
 * Copyright (c) 2019-2019 Link-Card.Co.Ltd. All rights reserved.
 *
 * Authors:
 *    JiangYong <1556631285@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * 'Software'), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Function:  menu display 
 * Created on: 2019-10-05
 */

#include <stdio.h>
#include <malloc.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <time.h>
#include <assert.h>
#include <libgen.h>

#include "menu.h"
#include "event.h"

#include "log2file.h"

static void Texture_loaging(int x, int y, SDL_Texture *texture, SDL_Renderer *ren)
{
	int w, h;
	SDL_Rect dst_rect;
	dst_rect.x = x;
	dst_rect.y = y;
	SDL_QueryTexture(texture, NULL, NULL, &w, &h);
	dst_rect.w = w;
	dst_rect.h = h;
	SDL_RenderCopy(ren, texture, NULL, &dst_rect);
}

// static void Text_loading_color(char *text, int x, int y, TTF_Font *tft_font, SDL_Renderer *ren , SDL_Color color )
// {
// 	SDL_Surface *surface = TTF_RenderUTF8_Blended(tft_font, text, color);
// 	SDL_Texture *texture = SDL_CreateTextureFromSurface(ren, surface);
// 	SDL_Rect dst_rect;
// 	int w, h;
// 	dst_rect.x = x;
// 	dst_rect.y = y;
// 	SDL_QueryTexture(texture, NULL, NULL, &w, &h);
// 	dst_rect.w = w;
// 	dst_rect.h = h;
// 	SDL_RenderCopy(ren, texture, NULL, &dst_rect);

// 	SDL_DestroyTexture(texture);
// 	SDL_FreeSurface(surface);
// }

static void init_menu_normal(icon_normal_t *p)
{
	if (NULL == p->not_connected_Texture) {
		/* 2-screen */
		p->not_connected_Texture = IMG_LoadTexture(p->renderer, "./Resources/2-screen/Not connected.png");

		/* common icon */
		p->wifi_signal_Texture[0] = IMG_LoadTexture(p->renderer, "./Resources/common icon/signal-1.png");
		p->wifi_signal_Texture[1] = IMG_LoadTexture(p->renderer, "./Resources/common icon/signal-2.png");
		p->wifi_signal_Texture[2] = IMG_LoadTexture(p->renderer, "./Resources/common icon/signal-3.png");
		p->wifi_signal_Texture[3] = IMG_LoadTexture(p->renderer, "./Resources/common icon/signal-4.png");
		p->wifi_signal_Texture[4] = IMG_LoadTexture(p->renderer, "./Resources/common icon/signal-not connected.png");
		p->wifi_signal_Texture[5] = IMG_LoadTexture(p->renderer, "./Resources/common icon/signal-refresh.png");
		
		/* 3-screen */
		p->tip_Texture = IMG_LoadTexture(p->renderer, "./Resources/3-screen/list of saved connections.png");
		p->icon_select_Texture = IMG_LoadTexture(p->renderer, "./Resources/3-screen/icon-select.png");
		p->bg_sel_Texture[0][0] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/icon-unfocus_middle.png");
		p->bg_sel_Texture[0][1] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/icon-unfocus_bottom.png");
		p->bg_sel_Texture[1][0] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/icon-focus_middle.png");
		p->bg_sel_Texture[1][1] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/icon-focus_bottom.png");
		p->bg_connect_Texture[0] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/connect-Disconnect-unfocus.png");
		p->bg_connect_Texture[1] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/connect-Disconnect-focus.png");
		p->list_wifi_signal_Texture[0] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/signal-1.png");
		p->list_wifi_signal_Texture[1] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/signal-2.png");
		p->list_wifi_signal_Texture[2] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/signal-3.png");
		p->list_wifi_signal_Texture[3] = IMG_LoadTexture(p->renderer, "./Resources/3-screen/signal-4.png");

		/* 4-screen */
		p->password_frame_Texture = IMG_LoadTexture(p->renderer, "./Resources/4-screen/password-frame.png");
		p->list_bar_sel_Texture[0][0] = IMG_LoadTexture(p->renderer, "./Resources/4-screen/list-bar-top-unfocus.png");
		p->list_bar_sel_Texture[0][1] = IMG_LoadTexture(p->renderer, "./Resources/4-screen/list-bar-middle-unfocus.png");
		p->list_bar_sel_Texture[0][2] = IMG_LoadTexture(p->renderer, "./Resources/4-screen/list-bar-bottom-unfocus.png");
		p->list_bar_sel_Texture[1][0] = IMG_LoadTexture(p->renderer, "./Resources/4-screen/list-bar-top.png");
		p->list_bar_sel_Texture[1][1] = IMG_LoadTexture(p->renderer, "./Resources/4-screen/list-bar-middle.png");
		p->list_bar_sel_Texture[1][2] = IMG_LoadTexture(p->renderer, "./Resources/4-screen/list-bar-bottom.png");
		/* Version */
		p->bg_ver_info_top_Texture = IMG_LoadTexture(p->renderer, "./Resources/Version/Product information.png");
		p->bg_ver_info_Texture = IMG_LoadTexture(p->renderer, "./Resources/Version/Product information_body.png");
		p->bg_update_Texture = IMG_LoadTexture(p->renderer, "./Resources/Version/UPDATE IN PROGRESS.png");
		p->update_sel_Texture[0] = IMG_LoadTexture(p->renderer, "./Resources/Version/Update-Exit-unfocus.png");
		p->update_sel_Texture[1] = IMG_LoadTexture(p->renderer, "./Resources/Version/Update-Exit-focus.png");

		p->tft_font_28 = TTF_OpenFont("./Resources/arial.ttf", 28);
		p->tft_font_24 = TTF_OpenFont("./Resources/arial.ttf", 24);
		p->tft_font_20 = TTF_OpenFont("./Resources/arial.ttf", 20);
		p->tft_font_19 = TTF_OpenFont("./Resources/arial.ttf", 19);
		p->tft_font_16 = TTF_OpenFont("./Resources/arial.ttf", 16);
	}
}

void update_menu_normal(icon_normal_t *menu, void *pso)
{
	SDL_Color color = {255, 255, 255, 255};
	// char text[256] = {0};
	SDL_Surface *surface;
	SDL_Texture *texture;
	SDL_Rect dst_rect;

	UICtx_t *pobj = (UICtx_t*)pso;
	int wifi_signal_level = pobj->devctx.wifi_signal_level.val;
	int wifi_status = pobj->devctx.wifi_status.val;
	int wifi = pobj->devctx.wifi.val;
	int language = pobj->devctx.language.val;

	init_menu_normal(menu);
	
	/* 没有连接，没有WiFi热点 */
	if (wifi) {
		if (!wifi_status) {
			/* Not connected */
			SDL_QueryTexture(menu->not_connected_Texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
			dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
			dst_rect.y = (OSD_HEIGHT - dst_rect.h) / 2;
			SDL_RenderCopy(menu->renderer, menu->not_connected_Texture, NULL, &dst_rect);

			char text_tip[][64] = {"НЕТ ПОДКЛЮЧЕНИЯ", "Not connected"};
			surface = TTF_RenderUTF8_Blended(menu->tft_font_20, text_tip[language], color);
			texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
			SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
			dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
			dst_rect.y = (OSD_HEIGHT - dst_rect.h) / 2;
			SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
			SDL_DestroyTexture(texture);
			SDL_FreeSurface(surface);
		} else {
			Texture_loaging(150, 18, menu->wifi_signal_Texture[wifi_signal_level], menu->renderer);
		}
	}
}

void update_menu_search_wifi(icon_normal_t *menu, void *pso)
{
	SDL_Color color = {255, 255, 255, 255};
	char text[256] = {0};
	SDL_Surface *surface;
	SDL_Texture *texture;
	SDL_Rect dst_rect;

	UICtx_t *pobj = (UICtx_t*)pso;
	int wifi_signal_level = pobj->devctx.wifi_signal_level.val;
	int wifi_connect_status = pobj->devctx.wifi_connect_status.val;
	int status_sel = pobj->devctx.status_sel.val;
	int wifi_index = pobj->devctx.wifi_index.val;
	int wifi_index_max = pobj->devctx.wifi_index.max;
	int wifi_page_index = pobj->devctx.wifi_page_index.val;
	int language = pobj->devctx.language.val;
	int pos, i;

	init_menu_normal(menu);

	/* wifi signal */
	Texture_loaging(150, 18, menu->wifi_signal_Texture[wifi_signal_level], menu->renderer);

	/* list connections */
	int pos_y = 180;//70;
	SDL_QueryTexture(menu->tip_Texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
	dst_rect.y = pos_y;
	SDL_RenderCopy(menu->renderer, menu->tip_Texture, NULL, &dst_rect);

	int pos_x = dst_rect.x;
	int tip_icon_w = dst_rect.w;
	int tip_icon_h = dst_rect.h;

	TTF_SetFontStyle(menu->tft_font_24, TTF_STYLE_BOLD);	// 设置粗体
	char text_title[][64] = {"Список соединений", "list connections"};
	surface = TTF_RenderUTF8_Blended(menu->tft_font_20, text_title[language], color);
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = pos_x + (tip_icon_w - dst_rect.w) / 2;
	dst_rect.y = pos_y + (tip_icon_h - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);
	TTF_SetFontStyle(menu->tft_font_24, TTF_STYLE_NORMAL);

	if (1 == wifi_connect_status) {	// 选中WiFi热点
		Texture_loaging(pos_x, pos_y + tip_icon_h * (wifi_index + 1), menu->icon_select_Texture, menu->renderer);
	}
	for (i = 0; i < 6; i++) {
		pos = wifi_page_index * 6 + i;
		if (pos < wifi_index_max) {
			struct sNode wifi_node;
			getElem(pobj->devctx.list_head, pos + 1, &wifi_node);

			/* bg */
			SDL_QueryTexture(menu->bg_sel_Texture[i == wifi_index][i == 5 || pos == (wifi_index_max - 1)], NULL, NULL, &dst_rect.w, &dst_rect.h);
			dst_rect.x = pos_x;
			dst_rect.y = pos_y + tip_icon_h * (i + 1);
			SDL_RenderCopy(menu->renderer, menu->bg_sel_Texture[i == wifi_index][i == 5 || pos == (wifi_index_max - 1)], NULL, &dst_rect);

			/* wifi signal */
			SDL_QueryTexture(menu->list_wifi_signal_Texture[wifi_signal_level_switch(wifi_node.signal_level)], NULL, NULL, &dst_rect.w, &dst_rect.h);
			dst_rect.x = pos_x + 18;
			dst_rect.y = pos_y + tip_icon_h * (i + 1) + (tip_icon_h - dst_rect.h) / 2;
			SDL_RenderCopy(menu->renderer, menu->list_wifi_signal_Texture[wifi_signal_level_switch(wifi_node.signal_level)], NULL, &dst_rect);
			
			/* wifi signal图标的宽 */
			int wifi_signal_icon_w = dst_rect.w;

			/* WiFi ssid */
			memset(text, 0, sizeof(text));
			strncpy(text, wifi_node.essid, 20);
			surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text, color);
			texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
			SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
			dst_rect.x = dst_rect.x + wifi_signal_icon_w + 15;
			dst_rect.y = pos_y + tip_icon_h * (i + 1) + (tip_icon_h - dst_rect.h) / 2;
			SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
			SDL_DestroyTexture(texture);
			SDL_FreeSurface(surface);
		} else {
			break;
		}
	}

	/* connect */
	SDL_QueryTexture(menu->bg_connect_Texture[(0 == wifi_connect_status) ? 0 : (0 == status_sel)], NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = pos_x;
	dst_rect.y = pos_y + tip_icon_h * (i + 1) + 25;
	SDL_RenderCopy(menu->renderer, menu->bg_connect_Texture[(0 == wifi_connect_status) ? 0 : (0 == status_sel)], NULL, &dst_rect);
	
	int bg_connect_icon_w = dst_rect.w;
	int bg_connect_icon_h = dst_rect.h;

	char text_connect[][64] = {"Подключить", "Connect"};
	surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text_connect[language], color);
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = dst_rect.x + (bg_connect_icon_w - dst_rect.w) / 2;
	dst_rect.y = dst_rect.y + (bg_connect_icon_h - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);

	/* disconnect */
	SDL_QueryTexture(menu->bg_connect_Texture[(0 == wifi_connect_status) ? 0 : (1 == status_sel)], NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = pos_x + tip_icon_w - dst_rect.w;
	dst_rect.y = pos_y + tip_icon_h * (i + 1) + 25;
	SDL_RenderCopy(menu->renderer, menu->bg_connect_Texture[(0 == wifi_connect_status) ? 0 : (1 == status_sel)], NULL, &dst_rect);

	char text_disconnect[][64] = {"Отключить", "Disconnect"};
	surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text_disconnect[language], color);
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = dst_rect.x + (bg_connect_icon_w - dst_rect.w) / 2;
	dst_rect.y = dst_rect.y + (bg_connect_icon_h - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);
}

void update_menu_password(icon_normal_t *menu, void *pso)
{
	SDL_Color color[2] = {{128, 128, 128, 255}, {255, 255, 255, 255}};
	char text[256] = {0};
    SDL_Surface *surface;
    SDL_Texture *texture;
    SDL_Rect dst_rect;

	UICtx_t *pobj = (UICtx_t*)pso;
	int wifi_password_sel = pobj->devctx.wifi_password_sel.val;
	int password_enter_num = pobj->devctx.password_enter_num.val;

	init_menu_normal(menu);

	int pos_x = 338;//210;
	int list_bar_icon_w, list_bar_icon_h;
	for (int i = 0; i < 11; i++) {
		SDL_QueryTexture(menu->list_bar_sel_Texture[wifi_password_sel == i][0 == i ? 0 : (10 == i ? 2 : 1)], NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = pos_x;
		dst_rect.y = (OSD_HEIGHT - dst_rect.h * 11) / 2 + dst_rect.h * i;
		SDL_RenderCopy(menu->renderer, menu->list_bar_sel_Texture[wifi_password_sel == i][0 == i ? 0 : (10 == i ? 2 : 1)], NULL, &dst_rect);
		
		list_bar_icon_w = dst_rect.w;
		list_bar_icon_h = dst_rect.h;
		if (i == 10) {
			sprintf(text, "OK");
		} else {
			sprintf(text, "%d", i == 9 ? 0 : i + 1);
		}
		surface = TTF_RenderText_Solid(menu->tft_font_16, text, color[(8 != password_enter_num && 10 == i) ? 0 : 1]);
		texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
		SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = pos_x + (list_bar_icon_w - dst_rect.w) / 2;
		dst_rect.y = dst_rect.y + (list_bar_icon_h - dst_rect.h) / 2;
		SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(surface);
	}

	/* 密码显示 */
	pos_x = pos_x + list_bar_icon_w + 50;//320;
	SDL_QueryTexture(menu->password_frame_Texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = pos_x;
	dst_rect.y = (OSD_HEIGHT - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, menu->password_frame_Texture, NULL, &dst_rect);

	int password_frame_icon_w = dst_rect.w;
	int password_frame_icon_h = dst_rect.h;

	for (int i = 0; i < password_enter_num; i++) {
		sprintf(text, "*");
		surface = TTF_RenderText_Solid(menu->tft_font_24, text, color[1]);
		texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
		SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = pos_x + (password_frame_icon_w - dst_rect.w * 8) / 9 * (i + 1) + dst_rect.w * i;
		dst_rect.y = (OSD_HEIGHT - password_frame_icon_h) / 2 + 27;
		SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(surface);
	}

	for (int i = 0; i < ((8 == password_enter_num) ? 8 : (password_enter_num + 1)); i++) {
		sprintf(text, "_");
		surface = TTF_RenderText_Solid(menu->tft_font_24, text, color[1]);
		texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
		SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = pos_x + (password_frame_icon_w - dst_rect.w * 8) / 9 * (i + 1) + dst_rect.w * i;
		dst_rect.y = (OSD_HEIGHT - password_frame_icon_h) / 2 + 20;
		SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(surface);
	}
}

void update_menu_ver_info(icon_normal_t *menu, void *pso)
{
	SDL_Color color = {255, 255, 255, 255};
	char text[256] = {0};
	SDL_Surface *surface;
	SDL_Texture *texture;
	SDL_Rect dst_rect;
	int pos_x, pos_y;

	UICtx_t *pobj = (UICtx_t*)pso;
	// int wifi_signal_level = pobj->devctx.wifi_signal_level.val;
	int status_sel = pobj->devctx.status_sel.val;
	int enter_update = pobj->devctx.enter_update.val;
	int language = pobj->devctx.language.val;

	init_menu_normal(menu);

	// /* wifi sinal */
	// Texture_loaging(120, 15, menu->wifi_signal_Texture[wifi_signal_level], menu->renderer);

	pos_y = 158;
	/* ver info bg top */
	SDL_QueryTexture(menu->bg_ver_info_top_Texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
	dst_rect.y = pos_y;
	SDL_RenderCopy(menu->renderer, menu->bg_ver_info_top_Texture, NULL, &dst_rect);

	pos_x = dst_rect.x;
	int bg_ver_info_top_icon_w = dst_rect.w;
	int bg_ver_info_top_icon_h = dst_rect.h;
	
	TTF_SetFontStyle(menu->tft_font_24, TTF_STYLE_BOLD);	// 设置粗体
	char text_title[][64] = {"Информация о продукте", "Product information"};
	surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text_title[language], color);
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
	dst_rect.y = pos_y + (bg_ver_info_top_icon_h - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);
	TTF_SetFontStyle(menu->tft_font_24, TTF_STYLE_NORMAL);

	/* ver info bg */
	SDL_QueryTexture(menu->bg_ver_info_Texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
	dst_rect.y = pos_y + bg_ver_info_top_icon_h;
	SDL_RenderCopy(menu->renderer, menu->bg_ver_info_Texture, NULL, &dst_rect);

	int bg_ver_info_icon_h = dst_rect.h;

	for (int i = 0; i < 5; i++) {
		switch (i)
		{
		case 0:
			switch (language)
			{
			case RUSSIAN:
				sprintf(text, "ID:ЗЕНИТКА");
				break;
			case ENGLISH:
				sprintf(text, "ID:ZENITCO");
				break;
			}
			break;
		case 1:
			sprintf(text, "SN:%s", pobj->info.sn);
			break;
		case 2:
			sprintf(text, "GUI Version:%s", pobj->info.gui_ver);
			break;
		case 3:
			sprintf(text, "HW Version:%s", pobj->info.hw_ver);
			break;
		case 4:
			sprintf(text, "SYS Version:%s", pobj->info.sys_ver);
			break;
		}
		surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text, color);
		texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
		SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = pos_x + 45;
		dst_rect.y = pos_y + bg_ver_info_top_icon_h + 55 + 45 * i;
		SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(surface);
	}

	/* update */
	SDL_QueryTexture(menu->update_sel_Texture[(0 == enter_update) ? 0 : (0 == status_sel)], NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = pos_x;
	dst_rect.y = pos_y + bg_ver_info_top_icon_h + bg_ver_info_icon_h + 40;
	SDL_RenderCopy(menu->renderer, menu->update_sel_Texture[(0 == enter_update) ? 0 : (0 == status_sel)], NULL, &dst_rect);
	
	int update_sel_icon_w = dst_rect.w;
	int update_sel_icon_h = dst_rect.h;

	char text_update[][64] = {"Обновить", "Update"};
	surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text_update[language], color);
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = dst_rect.x + (update_sel_icon_w - dst_rect.w) / 2;
	dst_rect.y = dst_rect.y + (update_sel_icon_h - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);

	/* exit */
	SDL_QueryTexture(menu->update_sel_Texture[(0 == enter_update) ? 0 : (0 == status_sel)], NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = pos_x + bg_ver_info_top_icon_w - dst_rect.w;
	dst_rect.y = pos_y + bg_ver_info_top_icon_h + bg_ver_info_icon_h + 40;
	SDL_RenderCopy(menu->renderer, menu->update_sel_Texture[(0 == enter_update) ? 0 : (1 == status_sel)], NULL, &dst_rect);

	char text_exit[][64] = {"ВЫХОД", "Exit"};
	surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text_exit[language], color);
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = dst_rect.x + (update_sel_icon_w - dst_rect.w) / 2;
	dst_rect.y = dst_rect.y + (update_sel_icon_h - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);
}

void update_menu_update(icon_normal_t *menu, void *pso)
{
	SDL_Color color = {255, 255, 255, 255};
	char text[256] = {0};
	SDL_Surface *surface;
	SDL_Texture *texture;
	SDL_Rect dst_rect;

	UICtx_t *pobj = (UICtx_t*)pso;
	int language = pobj->devctx.language.val;

	init_menu_normal(menu);

	/* bg */
	SDL_QueryTexture(menu->bg_update_Texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
	dst_rect.y = (OSD_HEIGHT - dst_rect.h) / 2;
	SDL_RenderCopy(menu->renderer, menu->bg_update_Texture, NULL, &dst_rect);

	TTF_SetFontStyle(menu->tft_font_24, TTF_STYLE_BOLD);	// 设置粗体

	char text_title[][64] = {"В процессе обновления", "UPDATE IN PROGRESS"};
	surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text_title[language], color);
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
	dst_rect.y = dst_rect.y + 50;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);

	TTF_SetFontStyle(menu->tft_font_24, TTF_STYLE_NORMAL);

	char text_tip1[][128] = {"Не выключайте устройство во время обновления,", "Do not power off the device during update"};
	if (RUSSIAN == language) {
		surface = TTF_RenderUTF8_Blended(menu->tft_font_19, text_tip1[language], color);
	} else {
		surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text_tip1[language], color);
	}
	texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
	SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
	dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
	dst_rect.y = dst_rect.y + 60;
	SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
	SDL_DestroyTexture(texture);
	SDL_FreeSurface(surface);

	switch (language)
	{
	case RUSSIAN:
		sprintf(text, "После успешного обновления,");
		surface = TTF_RenderUTF8_Blended(menu->tft_font_19, text, color);
		texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
		SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
		dst_rect.y = dst_rect.y + 30;
		SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(surface);

		sprintf(text, "устройство перезагрузится.");
		surface = TTF_RenderUTF8_Blended(menu->tft_font_19, text, color);
		texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
		SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
		dst_rect.y = dst_rect.y + 30;
		SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(surface);
		break;
	case ENGLISH:
		sprintf(text, "if everything goes well,the device will restart");
		surface = TTF_RenderUTF8_Blended(menu->tft_font_24, text, color);
		texture = SDL_CreateTextureFromSurface(menu->renderer, surface);
		SDL_QueryTexture(texture, NULL, NULL, &dst_rect.w, &dst_rect.h);
		dst_rect.x = (OSD_WIDTH - dst_rect.w) / 2;
		dst_rect.y = dst_rect.y + 30;
		SDL_RenderCopy(menu->renderer, texture, NULL, &dst_rect);
		SDL_DestroyTexture(texture);
		SDL_FreeSurface(surface);
		break;
	}
}